package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.config.MyConfig;
import com.oceanread.cn.config.Result;
import com.oceanread.cn.entity.WaterDepth;
import com.oceanread.cn.service.IWaterDepthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

import static com.oceanread.cn.controller.Drillhole1Controller.uploadFile;

@RestController
@RequestMapping("/WaterDepth")
@CrossOrigin()
@Api(value = "", tags = {"2.6-水深信息"})
public class WaterDepthController {

    @Autowired
    private IWaterDepthService iWaterDepthService;

    /*获取总表*/
    @GetMapping("/getall")
    public List<WaterDepth> getList() {
        return iWaterDepthService.getall();
    }

    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iWaterDepthService.getTablelist(page, size));
        object.put("total", iWaterDepthService.getnum());
        return object;
    }

    @GetMapping("/query1")
    public JSONObject query(
            @RequestParam(name = "page", required = true, defaultValue = "1") int page,
            @RequestParam(name = "size", required = true, defaultValue = "10") int size,
            @RequestParam(name = "key") String key,
            @RequestParam(name = "value") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iWaterDepthService.query(page, size, key, value));
        object.put("total", iWaterDepthService.getnumquery(key, value));
        return object;
    }

    @ApiOperation(value = "前端可视化 获取数据URl")
    @GetMapping("/getinfo")
    public List<WaterDepth> equal(
            @RequestParam(name = "PrjCode") String PrjCode
    ) {
        return iWaterDepthService.equal("PrjCode", PrjCode);
    }

    @PostMapping("/update")
    public Integer update(@RequestBody WaterDepth waterDepth) {
        return iWaterDepthService.update(waterDepth);
    }

    @PostMapping("/insert")
    public Integer insert(@RequestBody WaterDepth waterDepth) {
        return iWaterDepthService.insert(waterDepth.getPrjCode(), waterDepth.getCode(), waterDepth.getName(), waterDepth.getRegion(),
                waterDepth.getTime(), waterDepth.getCenterCoordinateX(), waterDepth.getCenterCoordinateY(), waterDepth.getCenterCoordinateZ(),
                waterDepth.getMarkpath(), waterDepth.getTerrainpath());
    }

    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "sysid") int sysid) {
        return iWaterDepthService.delete(sysid);
    }

    @RequestMapping(value = "/uploadPhoto")
    @ResponseBody
    public Result uploadPhoto(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //设置文件上传路径
        String filePath = MyConfig.getFilepath() + "image/waterrender/";
        System.out.println(filePath + fileName);
        try {
            uploadFile(file.getBytes(), filePath, fileName);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    @RequestMapping(value = "/getPhoto/{imgUrl}", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getPhoto(@PathVariable("imgUrl") String imgUrl) {
        try {
            System.out.println(imgUrl);
            File file = new File(MyConfig.getFilepath() + "image/waterrender/" + imgUrl);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/uploadGeojson", method = RequestMethod.POST)
    @ResponseBody
    public Result uploadGeojson(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String filetype = fileName.substring(fileName.lastIndexOf("."));
        if (filetype.equals(".json"))//处理xls文件
        {
            try {
                String rootPath = MyConfig.getFilepath() + "json/waterrender/";
                String storagePath = rootPath + file.getOriginalFilename();
                /*保存文件*/
                Streams.copy(file.getInputStream(), new FileOutputStream(storagePath), true);

                System.out.println(storagePath);
                return Result.success();
            } catch (IOException e) {
                return Result.error(e.getMessage());
            }
        }
        return Result.error();
    }

    @RequestMapping(value = "/getGeojsonfile/{JsonName}", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public byte[] getGeojsonfile(@PathVariable("JsonName") String JsonName) {
        try {
            System.out.println(JsonName);
            File file = new File(MyConfig.getFilepath() + "json/waterrender/" + JsonName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }

    @ApiOperation(value = "WaterDepth统一插入")
    @RequestMapping(value = "/WaterDepthInsert", method = RequestMethod.POST)
    public void inserttest(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            if (reader != null) {
                String redaline = reader.readLine();
                JSONObject readobject = JSONObject.parseObject(redaline);
                JSONObject data = readobject.getJSONObject("data");
                System.out.println(data);
                String PrjCode = data.getString("PrjCode");
                String Name = data.getString("Name");

                String Markpath = data.getString("Markpath");
                String Terrainpath = data.getString("Terrainpath");
                WaterDepth waterDepth = new WaterDepth();
                waterDepth.setPrjCode(PrjCode);
                waterDepth.setName(Name);
                waterDepth.setMarkpath(Markpath);
                waterDepth.setTerrainpath(Terrainpath);
                iWaterDepthService.insert(waterDepth.getPrjCode(), waterDepth.getCode(), waterDepth.getName(), waterDepth.getRegion(),
                        waterDepth.getTime(), waterDepth.getCenterCoordinateX(), waterDepth.getCenterCoordinateY(), waterDepth.getCenterCoordinateZ(),
                        waterDepth.getMarkpath(), waterDepth.getTerrainpath()
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
